/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.widgets;

import java.text.DecimalFormat;
import java.text.ParseException;
import javax.swing.text.DefaultFormatter;

/**
 *
 * @author david
 */
public class DoubleFormatter extends DefaultFormatter {

    private DecimalFormat TWO_PLACES = new DecimalFormat("0.##");

    @Override
    public Object stringToValue(String string) throws ParseException {
        
        try {
            return Double.parseDouble(string);

        } catch (NumberFormatException e) {
            invalidEdit();
            return null;
        }
    }

    @Override
    public String valueToString(Object value) throws ParseException {

        if (value == null) {
            return "";
        }

        Number number = (Number)value;
        return TWO_PLACES.format(number);
    }
}
